package com.thx.workfloworguser.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thx.common.util.AppContext;
import com.thx.dictionary.dao.DictionaryDao;
import com.thx.dictionary.dao.DictionaryValueDao;
import com.thx.org.dao.CompanyDao;
import com.thx.org.dao.DepartmentDao;
import com.thx.org.model.Company;
import com.thx.role.dao.RoleDao;
import com.thx.user.dao.UserDao;
import com.thx.workfloworguser.dao.DepartmentImplDao;
import com.thx.workfloworguser.dao.RoleImplDao;
import com.thx.workfloworguser.dao.UserImplDao;
import com.thx.workfloworguser.model.DepartmentImpl;
import com.thx.workfloworguser.model.DictionaryImpl;
import com.thx.workfloworguser.model.DictionaryValueImpl;
import com.thx.workfloworguser.model.OptionImpl;
import com.thx.workfloworguser.model.RoleImpl;
import com.thx.workfloworguser.model.UserImpl;
import com.topdt.jbpm.dictionary.Dictionary;
import com.topdt.jbpm.dictionary.DictionaryValue;
import com.topdt.jbpm.form.query.Option;
import com.topdt.jbpm.identity.Department;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.Role;
import com.topdt.jbpm.identity.User;

/**
 * @Project : thx-workflow-orguser
 * @Program Name : com.thx.workfloworguser.service.IdentityServiceImpl.java
 * @Class Name : IdentityServiceImpl
 * @Description : 类描述
 * @Author : calonlan
 * @Creation Date : 2015年1月23日 下午2:48:39
 * @ModificationHistory Who When What -------- ----------
 *                      ----------------------------------- username 2015年1月23日
 *                      TODO
 */
@Service
@Transactional(readOnly = true)
public class IdentityServiceImpl implements IdentityService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserImplDao userImplDao;
	@Autowired
	private DepartmentDao departmentDao;
	@Autowired
	private DepartmentImplDao departmentImplDao;
	@Autowired
	private RoleImplDao roleImplDao;
	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private DictionaryDao dictDao;
	@Autowired
	private DictionaryValueDao dictValueDao;

	@Override
	public List<User> getUsersByRole(String... roleId) {
		return null;
	}

	/**
	 * @Description : 根据部门id集合得到人员
	 * @Method_Name : getUsersByDep
	 * @param depId
	 * @return
	 * @Creation Date : 2015年4月29日 上午11:20:55
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<User> getUsersByDep(String... depId) {
		List<com.thx.user.model.User> allUsers = new ArrayList<com.thx.user.model.User>();
		for (String temp : depId) {
			List<com.thx.user.model.User> users = userDao.findByDeptId(temp);
			allUsers.addAll(users);

		}
		UserImpl userImpl = null;
		List<User> userImpls = new ArrayList<User>();
		for (com.thx.user.model.User user : allUsers) {
			userImpl = new UserImpl();
			userImpl.setUser(user);
			userImpls.add(userImpl);
		}
		return userImpls;
	}

	@Override
	public List<User> getUsersByRoleAndDep(String roleId, String depId) {
		// 得到角色对应的人员
		Set<com.thx.user.model.User> usersOfRole = roleDao.get(roleId).getUsers();
		// 得到部门对应的人员
		List<com.thx.user.model.User> usersOfDept = null;
		if (depId.equals(AppContext.getCurrentComId())) {
			usersOfDept = userDao.findByDeptId(null);
		} else {
			usersOfDept = userDao.findByDeptId(depId);
		}
		// 取用户的交集
		List<User> users = new ArrayList<User>();
		for (com.thx.user.model.User temp : usersOfRole) {
			for (com.thx.user.model.User tem : usersOfDept) {
				if (temp.getUserId().equals(tem.getUserId())) {
					// userImpl = new UserImpl();
					// 设置懒加载关联对象为null，否则json转换死循环，报错
					/*
					 * if (null != temp.getDepartment()) {
					 * temp.getDepartment().setUsers(null); }
					 * temp.setRoles(null); userImpl.setUser(temp);
					 */
					users.add(copyUser(temp));
				}
			}

		}
		return users;
	}

	/**
	 * @Description : 根据部门和角色集合得到人员(取部门人员和角色人员的交集)
	 * @Method_Name : getUsersByRoleAndDep
	 * @param roleIds
	 * @param depIds
	 * @return
	 * @Creation Date : 2015年4月29日 上午11:33:59
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<User> getUsersByRoleAndDep(String[] roleIds, String[] depIds) {
		// 根据角色查询用户
		List<com.thx.user.model.User> usersOfRole = new ArrayList<com.thx.user.model.User>();
		if (roleIds != null && roleIds.length > 0) {
			for (String roleId : roleIds) {
				Set<com.thx.user.model.User> usersTemp = roleDao.get(roleId).getUsers();
				usersOfRole.addAll(usersTemp);
			}
		}
		// 根据部门查询用户
		List<com.thx.user.model.User> usersOfDept = new ArrayList<com.thx.user.model.User>();
		if (depIds != null && depIds.length > 0) {
			for (String depId : depIds) {
				// 如果部门id等于公司id，重新给部门id赋值，以符合组织人员组件的api
				List<com.thx.user.model.User> usersTemp = null;
				if (depId.equals(AppContext.getCurrentComId())) {
					usersTemp = userDao.findByDeptId(null);
				} else {
					usersTemp = userDao.findByDeptId(depId);
				}
				usersOfDept.addAll(usersTemp);
			}
		}
		List<User> users = new ArrayList<User>();
		// 取用户的交集
		for (com.thx.user.model.User temp : usersOfRole) {
			for (com.thx.user.model.User tem : usersOfDept) {
				if (temp.getUserId().equals(tem.getUserId())) {
					users.add(copyUser(temp));
				}
			}

		}
		return users;
	}

	/**
	 * @Description : 取得给定部门类型下给定角色的人员集合 ，（没有使用）
	 * @Method_Name : getUsersByRoleAndDepType
	 * @param roleIds
	 * @param depTIds
	 * @return
	 * @Creation Date : 2015年4月28日 下午5:49:38
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<User> getUsersByRoleAndDepType(String[] roleIds, String[] depTIds) {
		return null;
	}

	@Override
	public List<Role> getRolesOfUser(String userId) {
		com.thx.user.model.User user = userDao.get(userId);
		Set<com.thx.role.model.Role> set = user.getRoles();
		List<Role> roles = new ArrayList<Role>();
		Iterator<com.thx.role.model.Role> ite = set.iterator();
		RoleImpl impl = null;
		while (ite.hasNext()) {
			com.thx.role.model.Role temp = ite.next();
			impl = new RoleImpl();
			impl.setRole(temp);
			roles.add(impl);
		}
		return roles;
	}

	/**
	 * @Description : 得到人员所在部门(包括兼职部门)， 暂时没有实现
	 * @Method_Name : getDepsOfUser
	 * @param userId
	 * @return
	 * @Creation Date : 2015年5月20日 上午10:20:52
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<Department> getDepsOfUser(String userId) {
		com.thx.org.model.Department dept = userDao.get(userId).getDepartment();
		DepartmentImpl impl = new DepartmentImpl();
		// dept.setUsers(null);
		if (dept == null) {
			dept = new com.thx.org.model.Department();
		}
		impl.setDepartment(dept);
		List<Department> list = new ArrayList<Department>();
		list.add(impl);
		return list;
	}

	/**
	 * @Description : 得到人员所在部门
	 * @Method_Name : getMajorDepOfUser
	 * @param userId
	 * @return
	 * @Creation Date : 2015年4月29日 上午11:10:24
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public Department getMajorDepOfUser(String userId) {
		com.thx.org.model.Department dept = userDao.get(userId).getDepartment();
		DepartmentImpl impl = new DepartmentImpl();
		// dept.setUsers(null);
		if (dept == null) {
			dept = new com.thx.org.model.Department();
		}
		impl.setDepartment(dept);
		return impl;
	}

	/**
	 * @Description : 得到上n级部门领导
	 * @Method_Name : getManagersByUser
	 * @param userId
	 * @param level
	 * @return
	 * @Creation Date : 2015年4月29日 下午4:48:39
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<? extends User> getManagersByUser(String userId, String level) {
		com.thx.user.model.User user = userDao.get(userId);
		com.thx.org.model.Department tempDept = departmentDao.get(user.getDepartment().getDepId());
		DepartmentImpl deptImpl = new DepartmentImpl();
		deptImpl.setDepartment(tempDept);
		Department dept = getUpDeptById(deptImpl, Integer.parseInt(level));
		List<com.thx.user.model.User> list = userImplDao.findManagersByDeptId(dept.getId());
		List<User> users = new ArrayList<User>();
		UserImpl impl = null;
		for (com.thx.user.model.User temp : list) {

			if (null != temp.getDepartment()) {
				temp.getDepartment().setUsers(null);
			}
			temp.setRoles(null);
			impl = new UserImpl();
			impl.setUser(temp);
			users.add(impl);
		}
		return users;
	}

	/**
	 * @Description : 得到上n级部门
	 * @Method_Name : getUpDeptById
	 * @param deptId
	 * @param level
	 * @return
	 * @return : Department
	 * @Creation Date : 2015年4月29日 上午10:10:40
	 * @version : v1.00
	 * @Author : calonlan
	 *
	 * @Update Date :
	 * @Update Author :
	 */
	private DepartmentImpl getUpDeptById(DepartmentImpl deptImpl, int level) {

		/*
		 * com.thx.org.model.Department temp = departmentDao.get(deptId);
		 * DepartmentImpl deptImpl = new DepartmentImpl();
		 * deptImpl.setDepartment(temp);
		 */

		if (level == 0) {
			return deptImpl;
		} else {
			// 已经公司节点的下级节点（部门根节点）
			if (null == deptImpl.getParentId() || deptImpl.getParentId().length() < 0) {
				com.thx.org.model.Department com = new com.thx.org.model.Department();
				Company company = companyDao.get(AppContext.getCurrentComId());
				com.setDepId(company.getComId());
				com.setDepName(company.getComName());

				DepartmentImpl comImpl = new DepartmentImpl();
				comImpl.setDepartment(com);
				return comImpl;
			}
			level--;
			com.thx.org.model.Department temp = departmentDao.get(deptImpl.getParentId());
			deptImpl.setDepartment(temp);
			deptImpl = getUpDeptById(deptImpl, level);

		}
		// return null;
		return deptImpl;

	}

	@Override
	public List<? extends User> getManagersByDep(String depId, String level) {
		return null;
	}

	/**
	 * @Description : 取人员的上级（没有使用）
	 * @Method_Name : getSuperiors
	 * @param userId
	 * @return
	 * @Creation Date : 2015年4月28日 下午5:54:22
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<User> getSuperiors(String userId) {

		return null;
	}

	/**
	 * @Description : 取人员的下级（没有使用）
	 * @Method_Name : getJuniors
	 * @param userId
	 * @return
	 * @Creation Date : 2015年4月28日 下午5:53:32
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<User> getJuniors(String userId) {
		return null;
	}

	@Override
	public User getUserById(String userId) {
		com.thx.user.model.User user = userDao.get(userId);
		/*
		 * UserImpl userImpl = new UserImpl(); //设置懒加载关联对象为null，否则json转换死循环，报错
		 * if (null != user.getDepartment()) {
		 * user.getDepartment().setUsers(null); } user.setRoles(null);
		 * userImpl.setUser(user);
		 */
		return copyUser(user);
	}

	/**
	 *
	 * @author _zili
	 * @Description:拷贝user
	 * @date 2015年4月30日 下午2:27:03
	 */
	public User copyUser(com.thx.user.model.User srcUser) {
		UserImpl userImpl = new UserImpl();
		if (srcUser == null) {
			return userImpl;
		} else {
			com.thx.user.model.User user = new com.thx.user.model.User();
			user.setUserId(srcUser.getUserId());
			user.setUserName(srcUser.getUserName());
			user.setRealName(srcUser.getRealName());
			user.setEmail(srcUser.getEmail());
			user.setMobilePhone(srcUser.getMobilePhone());
			com.thx.org.model.Department dept = new com.thx.org.model.Department();
			if (srcUser.getDepartment() != null) {
				dept.setDepId(srcUser.getDepartment().getDepId());
				dept.setDepName(srcUser.getDepartment().getDepName());
			}
			user.setDepartment(dept);
			userImpl.setUser(user);
			return userImpl;
		}
	}

	@Override
	public List<User> getUsersByUserIds(String[] userIds) {
		List<User> users = new ArrayList<User>();
		// UserImpl userImpl = null;
		if (userIds != null) {
			for (String userId : userIds) {
				com.thx.user.model.User user = userDao.get(userId);
				// userImpl = new UserImpl();
				// 设置懒加载关联对象为null，否则json转换死循环，报错
				// user.setDepartment(null);
				/*
				 * if (null != user.getDepartment()) {
				 * user.getDepartment().setUsers(null); } user.setRoles(null);
				 *
				 * userImpl.setUser(user);
				 */
				users.add(copyUser(user));
			}
		}
		return users;
	}

	@Override
	public User getUserByUserId(String userId) {
		com.thx.user.model.User user = userDao.get(userId);
		// UserImpl userImpl = new UserImpl();
		// 设置懒加载关联对象为null，否则json转换死循环，报错
		// user.setDepartment(null);
		/*
		 * if (null != user.getDepartment()) {
		 * user.getDepartment().setUsers(null); } user.setRoles(null);
		 * userImpl.setUser(user);
		 */
		return copyUser(user);
	}

	@Override
	public List<? extends Department> getChildrenOfDep(String depId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int countChildrenOfDep(String depId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Department> getAllDepsAndUsers(String comId) {
		Company com = companyDao.get(comId);
		DepartmentImpl dept = null;
		com.thx.org.model.Department department = null;
		if (com != null) {
			department = new com.thx.org.model.Department();
			department.setDepName(com.getComName());
			dept = new DepartmentImpl();
			dept.setDepartment(department);
			getChildrenDepAndUsers(dept, comId, true);
			// 查询完子节点以后，重新给deptId赋值
			dept.getDepartment().setDepId(comId);
			List<Department> deList = new ArrayList<Department>();
			deList.add(dept);
			// getOptionsByDepartment(deList);
			return deList;
		}
		return null;
	}

	private void getChildrenDepAndUsers(Department dep, String comId, boolean isCompany) {
		List<com.thx.org.model.Department> children = departmentDao.getByParentId(dep.getId());
		List<com.thx.user.model.User> users = null;
		if (isCompany) {
			// TODO 如果是公司节点的用户,根据部门id为空和公司id为comId(根部门即公司)
			users = userDao.findByDeptId("");
		} else {
			users = userDao.findByDeptId(dep.getId());
		}

		List<User> userList = new ArrayList<User>();
		UserImpl user = null;
		for (com.thx.user.model.User temp : users) {
			user = new UserImpl();
			user.setUser(temp);
			userList.add(user);
		}
		dep.setUsers(userList);

		if (children != null && children.size() > 0) {
			List<Department> listDept = new ArrayList<Department>();
			DepartmentImpl dept = null;
			for (com.thx.org.model.Department temp : children) {
				dept = new DepartmentImpl();
				dept.setDepartment(temp);
				listDept.add(dept);
			}
			dep.setChildren(listDept);
			for (Department child : listDept) {
				getChildrenDepAndUsers(child, comId, false);
			}
		}
	}

	private void getChildrenDept(Department dep) {
		List<com.thx.org.model.Department> children = departmentDao.getByParentId(dep.getId());
		if (children != null && children.size() > 0) {
			List<Department> listDept = new ArrayList<Department>();
			DepartmentImpl dept = null;
			for (com.thx.org.model.Department temp : children) {
				dept = new DepartmentImpl();
				dept.setDepartment(temp);
				listDept.add(dept);
			}
			dep.setChildren(listDept);
			for (Department child : listDept) {
				getChildrenDept(child);
			}
		}
	}

	@Override
	public List<Department> getDeps(String comId, int level) {
		return null;
	}

	@Override
	public List<Role> getAllRoles(String comId) {
		List<com.thx.role.model.Role> list = roleDao.getAll();
		List<Role> roles = new ArrayList<Role>();
		RoleImpl role = null;
		for (com.thx.role.model.Role temp : list) {
			role = new RoleImpl();
			temp.setUsers(null);
			role.setRole(temp);
			roles.add(role);
		}
		return roles;
	}

	@Override
	public List<Option> getOptionsByDepartment(List<Department> deps) {
		Department dept = deps.get(0);
		Option op = converDeps2Options(dept);

		List<Option> opsList = new ArrayList<Option>();
		opsList.add(op);
		return opsList;
	}

	@Override
	public List<Option> getOptionsByRoles(List<Role> roles) {
		Role role = roles.get(0);
		Option op = converRoles2Options(role);
		List<Option> opsList = new ArrayList<Option>();
		opsList.add(op);
		return opsList;
	}

	@Override
	public List<Department> getDeps(String comId) {
		Company com = companyDao.get(comId);
		DepartmentImpl dept = null;
		com.thx.org.model.Department department = null;
		if (com != null) {
			department = new com.thx.org.model.Department();
			// department.setDepId(com.getComId());
			department.setDepName(com.getComName());
			dept = new DepartmentImpl();
			dept.setDepartment(department);
			getChildrenDept(dept);
			// 查询完子节点以后，重新给deptId赋值
			dept.getDepartment().setDepId(comId);
			List<Department> deList = new ArrayList<Department>();
			deList.add(dept);
			// getOptionsByDepartment(deList);
			return deList;
		}
		return null;
	}

	/**
	 * @Description : 得到下级部门负责人
	 * @Method_Name : getDownDepMgrs
	 * @param userId
	 * @return
	 * @Creation Date : 2015年4月29日 上午10:12:00
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<? extends User> getDownDepMgrs(String userId) {
		com.thx.user.model.User user = userDao.get(userId);
		List<com.thx.org.model.Department> list = new ArrayList<com.thx.org.model.Department>();
		list.add(user.getDepartment());
		// 得到下级部门列表
		List<com.thx.org.model.Department> depts = getDownDeptById(list, 1);
		List<User> users = new ArrayList<User>();
		UserImpl impl = null;
		// 根据部门得到部门领导
		for (com.thx.org.model.Department temp : depts) {
			List<com.thx.user.model.User> userTemps = userImplDao.findManagersByDeptId(temp.getDepId());
			for (com.thx.user.model.User tem : userTemps) {
				/*
				 * impl = new UserImpl(); if (null != tem.getDepartment()) {
				 * tem.getDepartment().setUsers(null); } tem.setRoles(null);
				 * impl.setUser(tem);
				 */
				users.add(copyUser(tem));
			}
		}
		return users;
	}

	/**
	 * @Description : 根据部门得到下n级部门
	 * @Method_Name : getDownDeptById
	 * @param list
	 * @param level
	 *            0表示当前部门，1表示下一级部门
	 * @return
	 * @return : List<com.thx.org.model.Department>
	 * @Creation Date : 2015年4月29日 上午11:03:08
	 * @version : v1.00
	 * @Author : calonlan
	 *
	 * @Update Date :
	 * @Update Author :
	 */
	private List<com.thx.org.model.Department> getDownDeptById(List<com.thx.org.model.Department> list, int level) {
		if (level == 0) {
			return list;
		} else {
			List<com.thx.org.model.Department> temps = new ArrayList<com.thx.org.model.Department>();
			for (com.thx.org.model.Department temp : list) {
				List<com.thx.org.model.Department> depts = departmentDao.getByParentId(temp.getDepId());
				temps.addAll(depts);
			}
			level--;
			list = getDownDeptById(temps, level);
		}
		return list;

	}

	/**
	 * @Description : 得到当前用户所在部门的其他员工
	 * @Method_Name : getOthersOfDep
	 * @param userId
	 * @return
	 * @Creation Date : 2015年4月29日 上午9:56:02
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<? extends User> getOthersOfDep(String userId) {
		com.thx.org.model.Department dept = userDao.get(userId).getDepartment();
		List<com.thx.user.model.User> list = userDao.findByDeptId(dept.getDepId());
		List<UserImpl> users = new ArrayList<UserImpl>();
		UserImpl impl = null;
		for (com.thx.user.model.User temp : list) {
			if (!userId.equals(temp.getUserId())) {
				/*
				 * impl = new UserImpl(); if (null != temp.getDepartment()) {
				 * temp.getDepartment().setUsers(null); } temp.setRoles(null);
				 * impl.setUser(temp);
				 */
				impl = (UserImpl) copyUser(temp);
				users.add(impl);
			}
		}
		return users;
	}

	@Override
	public Department getUpDepByUser(String userId, String level) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @Description : 得到下级部门所有人
	 * @Method_Name : getDownDepsByUser
	 * @param userId
	 * @return
	 * @Creation Date : 2015年4月29日 上午11:06:53
	 * @version : v1.00
	 * @Author : calonlan
	 * @Update Date :
	 * @Update Author :
	 */
	@Override
	public List<? extends User> getDownDepsByUser(String userId) {
		com.thx.user.model.User user = userDao.get(userId);
		List<com.thx.org.model.Department> list = new ArrayList<com.thx.org.model.Department>();
		list.add(user.getDepartment());
		// 得到下级部门列表
		List<com.thx.org.model.Department> depts = getDownDeptById(list, 1);
		List<User> users = new ArrayList<User>();
		UserImpl impl = null;
		// 根据部门得到部门人员
		for (com.thx.org.model.Department temp : depts) {
			List<com.thx.user.model.User> userTemps = userDao.findByDeptId(temp.getDepId());
			for (com.thx.user.model.User tem : userTemps) {
				/*
				 * impl = new UserImpl(); if (null != tem.getDepartment()) {
				 * tem.getDepartment().setUsers(null); } tem.setRoles(null);
				 * impl.setUser(tem);
				 */
				impl = (UserImpl) copyUser(tem);
				users.add(impl);
			}
		}
		return users;
	}

	@Override
	public Department getUpDepByDep(String depId, String level) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<? extends Department> getDownDepsByDep(String depId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDeptNames(Long deptId) {
		return null;
	}

	private Option converDeps2Options(Department dep) {
		OptionImpl option = new OptionImpl();
		// option.setId("dep:"+dep.getId());
		option.setId(dep.getId());
		option.setName(dep.getName());
		option.setType("dep");
		List<? extends com.topdt.jbpm.identity.Department> children = dep.getChildren();

		List<Option> depsOP = new ArrayList<Option>();

		if (children != null) {
			for (com.topdt.jbpm.identity.Department depTemp : children) {
				Option op = converDeps2Options(depTemp);
				depsOP.add(op);
			}
		}

		List<? extends User> users = dep.getUsers();

		List<Option> userOp = new ArrayList<Option>();

		if (users != null) {
			for (User user : users) {
				OptionImpl op = new OptionImpl();
				// op.setId("per:"+user.getId());
				op.setId(user.getId());
				op.setName(user.getName());
				op.setType("per");
				userOp.add(op);
			}
		}
		List<Option> childrenOp = new ArrayList<Option>();

		childrenOp.addAll(depsOP);
		childrenOp.addAll(userOp);

		option.setChildren(childrenOp);

		return option;
	}

	private Option converRoles2Options(Role role) {
		OptionImpl option = new OptionImpl();
		option.setId(role.getId());
		option.setName(role.getName());
		List<? extends com.topdt.jbpm.identity.Role> children = role.getChildren();

		List<Option> rolesOP = new ArrayList<Option>();
		if (children != null) {
			for (com.topdt.jbpm.identity.Role roleTemp : children) {
				Option op = converRoles2Options(roleTemp);
				rolesOP.add(op);
			}
		}
		List<? extends User> users = role.getUsers();
		List<Option> userOp = new ArrayList<Option>();
		if (users != null) {
			for (User user : users) {
				OptionImpl op = new OptionImpl();
				op.setId(user.getId());
				op.setName(user.getName());
				userOp.add(op);
			}
		}
		List<Option> childrenOp = new ArrayList<Option>();
		childrenOp.addAll(rolesOP);
		childrenOp.addAll(userOp);
		option.setChildren(childrenOp);
		return option;
	}

	@Override
	public List<Role> getTreeRoles(String comId) {
		Company com = companyDao.get(comId);
		if (com != null) {
			RoleImpl impl = new RoleImpl();
			com.thx.role.model.Role role = new com.thx.role.model.Role();
			role.setRoleId(com.getComId());
			role.setRoleName(com.getComName());
			impl.setRole(role);
			getChildrenOfRole(impl);
			List<Role> roles = new ArrayList<Role>();
			roles.add(impl);
			return roles;
		}
		return null;
	}

	private void getChildrenOfRole(Role role) {
		List<com.thx.role.model.Role> tempRoles = roleDao.getAll();
		List<Role> roles = new ArrayList<Role>();
		RoleImpl impl = null;
		List<User> users = null;
		for (com.thx.role.model.Role temp : tempRoles) {
			impl = new RoleImpl();
			impl.setRole(temp);
			// 设置用户列表
			Set<com.thx.user.model.User> tempUsers = temp.getUsers();
			Iterator<com.thx.user.model.User> ite = tempUsers.iterator();
			users = new ArrayList<User>();
			UserImpl userImpl = null;
			while (ite.hasNext()) {
				com.thx.user.model.User tempUser = ite.next();
				userImpl = new UserImpl();
				userImpl.setUser(tempUser);
				users.add(userImpl);
			}

			impl.setUsers(users);
			roles.add(impl);

		}

		role.setChildren(roles);
	}

	@Override
	public Department getDeptById(String deptId) {
		com.thx.org.model.Department dept = departmentDao.get(deptId);
		DepartmentImpl impl = new DepartmentImpl();
		if (dept == null) {
			dept = new com.thx.org.model.Department();
		}
		impl.setDepartment(dept);
		return impl;
	}

	/*
	 * @Override public User getCurrentUser(HttpServletRequest request) {
	 * HttpSession session = request.getSession(); com.thx.user.model.User user
	 * = (com.thx.user.model.User) session.getAttribute(WebConstant.KEY_USER);
	 * UserImpl userImpl = new UserImpl(); userImpl.setUser(user);
	 *
	 * return userImpl; }
	 */
	@Override
	public List<Dictionary> searchByCode(String dictCode) {
		List<com.thx.dictionary.model.Dictionary> dictList = dictDao.searchByCode(dictCode);
		List<Dictionary> newDictList = new ArrayList<Dictionary>();
		DictionaryImpl newDict = null;
		for (com.thx.dictionary.model.Dictionary dict : dictList) {
			newDict = new DictionaryImpl();
			newDict.setDic(dict);
			newDictList.add(newDict);
		}
		return newDictList;
	}

	@Override
	public List<DictionaryValue> findByDicnId(String dicnId) {
		List<com.thx.dictionary.model.DictionaryValue> dictValueList = dictValueDao.findByDicnId(dicnId);
		List<DictionaryValue> newDictValueList = new ArrayList<DictionaryValue>();
		DictionaryValueImpl newDictValue = null;
		for (com.thx.dictionary.model.DictionaryValue dictValue : dictValueList) {
			newDictValue = new DictionaryValueImpl();
			newDictValue.setDicValue(dictValue);
			newDictValueList.add(newDictValue);
		}
		return newDictValueList;
	}

	@Override
	public Dictionary findDictById(String dicnId) {
		com.thx.dictionary.model.Dictionary dict = dictDao.get(dicnId);
		DictionaryImpl newDict = null;
		if (dict != null) {
			newDict = new DictionaryImpl();
			newDict.setDic(dict);
		}
		return newDict;
	}

	@Override
	public DictionaryValue findDictValueById(String dicvnId) {
		com.thx.dictionary.model.DictionaryValue dictValue = dictValueDao.get(dicvnId);
		DictionaryValueImpl newDict = null;
		if (dictValue != null) {
			newDict = new DictionaryValueImpl();
			newDict.setDicValue(dictValue);
		}
		return newDict;
	}

	@Override
	public com.topdt.jbpm.identity.Company getCompById(String comId) {
		Company com = companyDao.get(comId);
		com.thx.workfloworguser.model.CompanyImpl newCom = null;
		if (com != null) {
			newCom = new com.thx.workfloworguser.model.CompanyImpl();
			newCom.setComp(com);
		}
		return newCom;
	}
}
